var DoctorRevisit = {
    id: "DoctorRevisitTable",
    seItem: null,
    table: null,
    layerIndex: -1
};

function _cellStyles(value, row, index) {
    if (row.revisitStatus === 2) {
        return {css: {"background-color": "#fff1d5", "overflow": "hidden", "white-space": "nowrap", "text-overflow": "ellipsis"}}
    } else {
        return {css: {"overflow": "hidden", "white-space": "nowrap", "text-overflow": "ellipsis"}}
    }
}

DoctorRevisit.initColumn = function () {
    var columns = [
        {field: 'selectItem', radio: true},
        {title: 'customerId', field: 'customerId', visible: false, align: 'center', valign: 'middle'},
        {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: 'reservationsId', field: 'reservationsId', visible: false, align: 'center', valign: 'middle'},
        {
            title: '病历号', field: 'medicalRecordNumber', align: 'center', valign: 'middle', sortable: true, width: 120,
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '患者姓名', field: 'customerName', align: 'center', valign: 'middle', sortable: true, width: 120,
            formatter: function (value, row, index) {
                return "<a href='javascript:;' style='color: red;display: flex;align-items: center;' onclick='DoctorRevisit.lookDetail(" + '"' + row.customerId + '"' + ")'><img src='" + row.customerSexHead + "' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
            },
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '联系电话', field: 'customerPhone', align: 'center', valign: 'middle', width: 130,
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '随访医生', field: 'doctor', align: 'center', valign: 'middle', sortable: true, width: 110,
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '咨询师', field: 'fieldTc', align: 'center', valign: 'middle', sortable: true, width: 90,
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '创建人', field: 'createName', align: 'center', valign: 'middle', sortable: true, width: 90,
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '随访人员', field: 'revisit', align: 'center', valign: 'middle', sortable: true, width: 110,
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '回访内容', field: 'remarks', align: 'center', valign: 'middle', sortable: true, width: 200,
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '回访结果', field: 'revisitContent', align: 'center', valign: 'middle', width: 120,
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '计划随访时间', field: 'revisitTime', align: 'center', valign: 'middle', sortable: true, width: 110,
            formatter: function (value, row, index) {
                return value.substring(0, 11);
            },
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '回访状态', field: 'revisitStatus', align: 'center', valign: 'middle', sortable: true, width: 100,
            formatter: function (value, row, index) {
                return value === 1 ? '待计划' : value === 2 ? '回访完成' : value === 3 ? '回访未完成' : value === 4 ? '回访取消' : '';
            },
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '操作', field: 'id', align: 'center', valign: 'middle', width: 60,
            formatter: function (value, row, index) {
                var ids = row.id + "," + row.reservationsId + "," + row.customerId + "," + row.revisitTime;
                if(row.revisitStatus === 1){
                    return '<button title="编辑随访" style="background: #ffffff;border: 1px solid #cccccc;padding: 1px 4px 0px 5px;" onclick="DoctorRevisit.followRevision(' + "'" + ids + "'" + ')" type="button" class="btn"><span class="glyphicon glyphicon-pencil"></span></button>'
                }else if(row.revisitStatus === 2){
                    return '<button title="查看随访" style="background: #ffffff;border: 1px solid #cccccc;padding: 1px 4px 0px 5px;" onclick="DoctorRevisit.followRevision1(' + "'" + ids + "'" + ')" type="button" class="btn"><span class="glyphicon glyphicon-eye-open"></span></button>'
                }
           },
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        }
    ];
    return columns;
};

DoctorRevisit.lookDetail = function (id) {//点击姓名查看详情
    var index = layer.open({
        type: 2,
        title: '患者信息',
        area: ['100%', '100%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/doctorDetails/doctor_seeDiagnosis/' + id + '/' + 0
    });
    this.layerIndex = index;
};

DoctorRevisit.shortMessage = function () {//跳转到发送短信
    if (this.check()) {
        var customerId = DoctorRevisit.seItem.customerId;
        var index = layer.open({
            type: 2,
            title: '发送消息',
            area: ['60%', '80%'], //宽高
            fix: true, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/msgSend/to_msgSend/' + customerId
        });
        this.layerIndex = index;
    }
};

DoctorRevisit.followRevision = function (ids) {//点击表格随访按钮
    var index = layer.open({
        type: 2,
        title: '随访',
        area: ['60%', '100%'], //宽高
        fix: true, //不固定
        maxmin: true,
        offset: ['0px', '40%'],
        content: Feng.ctxPath + '/doctorRevisit/doctor_follow/' + ids
    });
    this.layerIndex = index;
};

DoctorRevisit.followRevision1 = function (ids) {//点击表格随访查看随访按钮
    var index = layer.open({
        type: 2,
        title: '随访',
        area: ['60%', '100%'], //宽高
        fix: true, //不固定
        maxmin: true,
        offset: ['0px', '40%'],
        content: Feng.ctxPath + '/doctorRevisit/look_doctor_followsd/' + ids
    });
    this.layerIndex = index;
};


DoctorRevisit.check = function () {// 检查是否选中
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        DoctorRevisit.seItem = selected[0];
        return true;
    }
};

DoctorRevisit.search = function () {//查询医生随访列表
    DoctorRevisit.table.refresh({query: DoctorRevisit.formParams()});
    DoctorRevisit.total();
};

DoctorRevisit.total = function () {//人数统计
    var reach = $("#doctorInputPublic", parent.document).val();
    var revisitStatus = $("#revisitStatus").val();  //回访状态
    var startRevisitTime = $("#startRevisitTime").val();  //开始回访时间
    var endRevisitTime = $("#endRevisitTime").val();  //结束回访时间
    var startCreateTime = $("#startCreateTime").val();  //开始创建时间
    var endCreateTime = $("#endCreateTime").val();  //结束创建时间
    var revisit = $("#revisit").val();  //随访人员
    var createName = $("#createName").val();  //创建人
    $.ajax({
        type: "GET",
        url: Feng.ctxPath + "/doctorRevisit/total",
        data: {
            reach: reach,
            revisitStatus: revisitStatus,
            startRevisitTime: startRevisitTime,
            endRevisitTime: endRevisitTime,
            startCreateTime: startCreateTime,
            endCreateTime: endCreateTime,
            revisit: revisit,
            createName: createName
        },
        dataType: "json",
        success: function (data) {
            $("#totalNum").html(data[0].countNum);//总人数
            $("#planned").html(data[1].countNum);//待计划
            $("#finished").html(data[2].countNum);//已完成
            $("#unfinished").html(data[3].countNum);//未完成
            $("#tocancel").html(data[4].countNum);//取消
        }
    });
};

DoctorRevisit.inquire = function () {//人数统计 + 表格刷新
    DoctorRevisit.search();
    DoctorRevisit.total();
};

DoctorRevisit.formParams = function () {//查询表单提交参数对象 @returns {{}}
    var queryData = {};
    queryData['reach'] = $("#doctorInputPublic", parent.document).val().trim();
    queryData['abbreviation'] = $("#abbreviation", parent.document).val();
    queryData['outpatientName'] = $("#outpatientName", parent.document).val();
    queryData['revisitStatus'] = $("#revisitStatus").val();
    queryData['startRevisitTime'] = $("#startRevisitTime").val();
    queryData['endRevisitTime'] = $("#endRevisitTime").val();
    queryData['startCreateTime'] = $("#startCreateTime").val();
    queryData['endCreateTime'] = $("#endCreateTime").val();
    queryData['revisit'] = $("#revisit").val();
    queryData['createName'] = $("#createName").val();
    console.log(queryData);
    return queryData;
};

DoctorRevisit.totalAllNumber = function (obj) {// 根据点击上面状态刷新表格
    $("#revisitStatus").val(obj);
    DoctorRevisit.inquire();
};

DoctorRevisit.resetSearch = function () {//重置
    $("#doctorInputPublic", parent.document).val("");
    $("#startRevisitTime").val("");
    $("#endRevisitTime").val("");
    $("#startCreateTime").val("");
    $("#endCreateTime").val("");
    $("#revisit").val("");
    $("#createName").val("");
    DoctorRevisit.inquire();
};

DoctorRevisit.time = function () {//初始化赋值时间 + 请求数据
    $("#startRevisitTime").val(setDateTime(new Date(), 0) + " " + "00:00:00");
    $("#endRevisitTime").val(setDateTime(new Date(), 0) + " " + "23:59:59");
    $("#revisitTimeTwo").val(setDateTime(new Date(), 0));
    var seleteDoctorValue = $('#loginName').val();
    $("#revisit option[value='" + seleteDoctorValue + "']").attr("selected", "selected");
};

$("#doctorSearchAll", parent.document).click(function () {//主页搜索
    DoctorRevisit.inquire();
});

function phonePrivileges(){   //隐藏手机号
    var phonePrivileges=parent.$("#phonePrivileges").val();
    if(phonePrivileges == 2){
        $('#DoctorRevisitTable').bootstrapTable('hideColumn', 'customerPhone');
    }
}
$(function () {
    var defaultColunms = DoctorRevisit.initColumn();
    var table = new BSTable(DoctorRevisit.id, "/doctorRevisit/list", defaultColunms);
    table.setPaginationType("server");
    table.setQueryParams(DoctorRevisit.formParams());
    DoctorRevisit.table = table.init();
    DoctorRevisit.time();
    DoctorRevisit.totalAllNumber();
    checkDate = function () {DoctorRevisit.inquire();};
    $(document).on('click', '#laydate_clear,#laydate_today,#laydate_ok', function () {DoctorRevisit.inquire();});
    phonePrivileges();
});